expire_fack_date={
	records:[{empid:'AE1856',
			name:'何宇林',
			category:'annual_leave',
			start_date:'2015-01-01',
			span:{value:'1.2',unit:'day'},
			used:{value:'1',unit:'day'},
			is_valid:false,
			note:'dsfsf'
			},
        {	empid:'AE1857',
        	name:'白联军',
        	category:'swap_off',
        	start_date:'2015-01-01',
        	span:{value:'20.5',unit:'hour'},
        	used:{value:'10',unit:'hour'},
        	is_valid:false,note:'dsfsf'
        	},
       ]
}

app.controller('expire_manage_controller',function($scope,$http,headFilterManager) {
	// 设置过滤器显示项
	$scope.fb={
		show:{
			keywords:true,
			only_valid:true
		},
		only_valid:true
	}
	
	// 设置所有头的过滤器 变量
	$scope.heads=[	{name:'empid',label:'员工号',filter:true},
					{name:'name',label:'姓名',filter:true},
					{name:'category',label:'假期类型',editor:'text',filter:true},
					{name:'start_date',label:'假期特征日期',editor:'text',filter:true},
					{name:'span',label:'长度',editor:'text'},
					{name:'used',label:'已使用'},
					{name:'is_valid',label:'是否有效',editor:'text',filter:true},
					{name:'note',label:'备注',editor:'text'},
					];
	
	$scope.currentPage=0;
	$scope.itemsPerPage=20;

	if(is_local){
		$scope.org_records=expire_fack_date.records
	}else{
		$scope.org_records=[];
	}
	$scope.records=$scope.org_records;
	//-------headfilter
	var hfmanagr=new headFilterManager.HFManager()
	hfmanagr.initHeads($scope.heads)
	$scope.$on('head_filter_changed',function (even,filter) {
		if(hfmanagr.has_filter(filter)){
			//$scope.records=$scope.funlist_filter($scope.org_records)
			$scope.records=hfmanagr.filter($scope.org_records)
		}
	})
	$scope.$on('sortClick',function (even,name) {
		$scope.$broadcast('otherSortClick',name);
	})

	$scope.upload_avai=function (file) {
		pro=file.upload("/attendance/upload/availeave/");
		pro.success(function (data) {
			if (data.status=='success'){
				alert('导入可用假期数据成功');
			}else{
				alert(data.msg)
			}
			
		}).error(function(data, status, headers, config) {
			alert('error '+status)
		}) 
	}
	
	$scope.search=function (start_date,end_date,fb) {
		var post_data = {
			filter_avai_leave:{start_date:start_date,end_date:end_date,keyword:fb.keywords,only_valid:fb.only_valid},
			};  
		$scope.fb.uploading=true;
		$http.post(post_url,post_data)
		.success(function(data, status, headers, config) { 
			$scope.org_records=data.filter_avai_leave.records;
			$scope.records=$scope.org_records;
			$scope.fb.uploading=false;
			$scope.currentPage=0
		}).error(function(data, status, headers, config) {  
		    alert('有错误,返回码为:'+status)
		    $scope.fb.uploading=false;
		});
	}
	
	$scope.update_to_server=function () {
		//if(!$scope.totaldiry)return
		$scope.uploading=true;
		var records=$scope.records;
		var postlist=[];
		for(x in records){
			if(records[x].dirty){
				postlist.push(records[x])
			}
		}
		var post_data = {
			update_availeaves:{records:postlist},
			};  
		$http.post(post_url,post_data)
		.success(function(data, status, headers, config) { 
			$scope.uploading=false;
			var updatedlist=data.update_availeaves.records;
			var old_records=$scope.org_records;
			for (x in updatedlist){
				for(y in old_records){
					if(old_records[y].id==updatedlist[x].id){
						old_records[y]=updatedlist[x];
						break;
					}
				}
			}
			$scope.records=$scope.org_records
			$scope.$broadcast('arrayUpdated',$scope.org_records)
			//on_org_records_new()
			
		}).error(function(data, status, headers, config) {  
		    //处理错误 
		    $scope.std='有错误,返回码为:'+status;
		});
	}
	$scope.refresh_annual=function () {
		$scope.uploading=true;
		var post_data = {
			refresh_annual:{},
			};  
		$http.post(post_url,post_data)
		.success(function(data, status, headers, config) { 
			$scope.uploading=false;
			alert('refresh success')
		}).error(function(data, status, headers, config) {  
		    alert('有错误,返回码为:'+status)
		});
	}			
})
